#ifndef __DRV_CAN0_H__
#define __DRV_CAN0_H__
#include <stdint.h>

#define CNA0_RX_NUMBER 16
// CANBUS
typedef struct CAN_ID_FORMAT_TAG
{
    uint32_t id; /*!<identifier */
    uint8_t ide; /*!< 0:standard format frame identifier;!0:extended identifier,  */
} CAN_ID_FORMAT_T;

typedef struct CAN_DATA_FORMAT_TAG
{
    uint32_t id;    /*!<identifier */
    uint8_t ide;    /*!< 0:standard format frame identifier;!0:extended identifier,  */
    uint8_t rx_len;
    uint8_t valid;  /*数据有效标志*/
    uint8_t rx_data[8];
} CAN_DATA_FORMAT_T;

int drv_can0_node_init(void);

int drv_can0_set_baudrate(uint16_t baudrate);
int DRV_CANBUS0_SET_RECEIVE_Filter(CAN_ID_FORMAT_T *id_tab, uint8_t id_num);
int DRV_CANBUS0_Enable(void);
int DRV_CANBUS0_Disable(void);
int DRV_CANBUS0_Send(uint32_t id, uint8_t ide, uint8_t *data, uint8_t length);
void DRV_CANBUS0_RECEIVE_Interrupt(CAN_DATA_FORMAT_T *p_msg);

void _DRV_CAN_STD_LIST_Filter(uint16_t filter_numb,uint16_t filter_fifo_numb,uint16_t *id_tab);

void _DRV_CAN_EXT_LIST_Filter(uint16_t filter_numb,uint16_t filter_fifo_numb,uint32_t *id_tab);

int drv_can0_nvic_config(void);
extern CAN_DATA_FORMAT_T receive_message_bootloader;
extern uint32_t ul_can0_total_rx_cnt;
extern uint32_t ul_can0_total_cnt_last;

extern void Can0_Data_Store_Fifo(uint32_t id,uint8_t *data, uint8_t length);
extern void Can0_Tx_Fifo(void);


/**
 * @brief can0注册标准帧ID，该函数放到参数初始化里面，即放到CAN初始化之前
 *
 * @param std_id 标准帧ID，最多注册56个ID，每4个ID为一组
 * @return int8_t >=0：组号，范围（0~13）；-1：超出最大列表数；-2：标准帧ID错误；
 */
int8_t can0_register_std_list_id(uint32_t std_id);
/**
 * @brief can0 注册扩展帧ID，该函数放到参数初始化里面，即放到CAN初始化之前;
 * 
 * @param ext_id 扩展帧ID，最多注册28个ID，每2个ID为一组
 * @return int8_t >=0：组号，范围（0~13）；-1：超出最大列表数；-2：扩展帧ID错误；
 * 注意：本函数必须放到can0_register_std_list_id函数之后
 */
int8_t can0_register_ext_list_id(uint32_t ext_id);

/**
 * @brief CAN0 注册标准帧列表和扩展帧列表
 */
void can0_register_list_id(void);
#endif
